<!DOCTYPE html>
<html class="x-admin-sm">

<head>
    <meta charset="UTF-8">
    <title>欢迎页面-X-admin2.2</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <link rel="shortcut icon" href="/static/img/favicon.ico">
    <link rel="stylesheet" href="/static/css/xadmin.css">
    <script src="/static/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="/static/js/xadmin.js"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
            <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
            <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
        <![endif]-->
</head>
<body>
<div class="layui-fluid">
    <div class="layui-row">
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <label class="layui-form-label">角色名称</label>
                <div class="layui-input-inline">
                    <input type="text" name="name" value="{{ role.name or '' }}" required lay-verify="required"
                           placeholder="请输入角色名称"
                           autocomplete="off" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">授权</label>
                <div class="layui-input-block">
                    <span id="test12" class="demo-tree-more"></span>
                    {#<input type="text" id="menu_ids" name="menu_ids">#}
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">状态</label>
                <div class="layui-input-block">
                    <input type="checkbox" id="statusId" name="status" value="1" lay-skin="switch" lay-filter="status"
                            {% if role is none or role.status == 1 %} checked {% endif %}>
                </div>
            </div>
            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">角色描述</label>
                <div class="layui-input-block">
                    <textarea name="describe" placeholder="请输入内容"
                              class="layui-textarea">{{ role.describe or '' }}</textarea>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-demo="getChecked" lay-submit="" lay-filter="add">立即提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>
    </div>
</div>
<script>

    layui.config({
        base: "/static/js/"
    }).use(['form', 'layer', 'common', 'tree', 'util'], function () {
        var $ = layui.jquery;
        var form = layui.form,
            layer = layui.layer,
            tree = layui.tree,
            util = layui.util,
            common = layui.common;

        //自定义验证规则
        form.verify({
            name: function (value) {
                if (value.length < 5) {
                    return '昵称至少得5个字符啊';
                }
            }
        });

        //基本演示
        tree.render({
            elem: '#test12'
            , data: {{ menus|tojson }}
            , showCheckbox: true  //是否显示复选框
            , id: 'treeId'
            , isJump: false //是否允许点击节点时弹出新窗口跳转
            , click: function (obj) {
                var data = obj.data;  //获取当前点击的节点数据
                //layer.msg('状态：' + obj.state + '<br>节点数据：' + JSON.stringify(data));
            }
        });


        //设置权限
        tree.setChecked('treeId', {{ role.menu_ids or [] }});
        var sub;
        //按钮事件
        util.event('lay-demo', {
            getChecked: function () {
                var checkedData = tree.getChecked('treeId'); //获取选中节点的数据
                sub = [];
                for (var i = 0; i < checkedData.length; i++) {
                    sub.push(checkedData[i].id);
                    if (checkedData[i].children.length > 0) {
                        $.each(checkedData[i].children, function (k, v) {
                            sub.push(v.id);
                            getsub(v);
                        });
                    }
                }
            }, setChecked: function () {

            }, reload: function () {
                //重载实例
                tree.reload('treeId', {});

            }
        });

        //递归获取菜单树id
        function getsub(obj) {
            if ("children" in obj && obj.children.length > 0) {
                $.each(obj.children, function (k, v) {
                    sub.push(v.id);
                    getsub(v);
                });
            }
            return sub;
        }

        //监听开关
        form.on('switch(status)', function () {
            if (this.checked) {
                $("#statusId").val("1");
            } else {
                $("#statusId").val("0");
            }
        });

        //监听提交
        form.on('submit(add)', function (data) {
            data.field.menu_ids = JSON.stringify(sub);
            //发异步，把数据提交
            var url = "/admin/role/{{ action }}/{{ role.id or null }}";
            var param = data.field;
            common.ajaxFormCms('post', 'false', url, param);

            //关闭当前frame
            xadmin.close();
            // 可以对父窗口进行刷新
            xadmin.father_reload();
            return false;
        });

    });
</script>

</body>
</html>
